package com.evolved.automata.android.bots;

import java.util.*;
import java.io.*;


public class StandardTools 
{
	
	
	
	
	public static void writeToFile(String logFileFullName, String text)
	{
		BufferedWriter writer=null;
		try
		{
			writer= new BufferedWriter(new FileWriter(logFileFullName,true));
			writer.write(text);
			writer.newLine();
		}
		catch (Exception e)
		{
			String s=getCurrentDateString();
		}
		finally
		{
			if (writer!=null)
			{
				try
				{
					writer.close();
				}
				catch (Exception e)
				{
					
				}
			}
		}
	}

	
	public static void mergeKeyValues(Hashtable<String,String> sourceMap, Hashtable<String,String> targetMap)
	{
		for (String sourceKey:sourceMap.keySet().toArray(new String[0]))
		{
			targetMap.put(sourceKey, sourceMap.get(sourceKey));
		}
	}

	public static void transferKeyValues(Hashtable<String,String> sourceMap, Hashtable<String,String> targetMap)
	{
		for (String keyName:sourceMap.keySet().toArray(new String[0]))
		{
			if (targetMap.containsKey(keyName))
				targetMap.put(keyName, sourceMap.get(keyName));
		}
	}
	
	// babyfish
	public static String[] extractQueryList(Hashtable<String, String> keyValueMap)
	{
		LinkedList<String> query = new LinkedList<String>();
		for (String keyname:keyValueMap.keySet().toArray(new String[0]))
		{
			if (keyValueMap.get(keyname).length()==0)
			{
				query.add(keyname);
				keyValueMap.remove(keyname);
			}
		}
		String[] queryList = new String[0];
		if (query.size()>0)
			queryList = query.toArray(new String[0]);
		
		return queryList;
	}
	
	
	
	
	
	
	public static int getCurrentDateInt()
	{
		Calendar calendar = Calendar.getInstance();
		int yearMultiplier=10^4;
		int monthMultiplier=10^2;
		int year = calendar.get(calendar.YEAR);
		int day = calendar.get(calendar.DAY_OF_MONTH)+1;
		int month = calendar.get(calendar.MONTH);
		int totalDate = year*yearMultiplier+month*monthMultiplier+day;
		return totalDate;
		
	}
	

	
	// TODO: Make this smarter
//	public static String getSeason(int dateInt)
//	{
//		
//		double base
//	}
	
	public static String getCurrentDateString()
	{
		Calendar calendar = Calendar.getInstance();
		int yearMultiplier=10000;
		int monthMultiplier=100;
		int year = calendar.get(calendar.YEAR);
		int day = calendar.get(calendar.DAY_OF_MONTH);
		int month = calendar.get(calendar.MONTH)+1;
		int totalDate = year*yearMultiplier+month*monthMultiplier+day;
		return ""+totalDate;
		
	}
	
	public static String mapValueToKey(Hashtable<String,String> map, String value)
	{
		String out=null;
		for (String key:map.keySet().toArray(new String[0]))
		{
			if (map.get(key).equals(value))
				return key;
		}
		return out;
	}
	
	public static String escapeDBString(String data)
	{
		if (data==null)
			return "";
		else
			return data.replace((CharSequence)"'", (CharSequence)"''");
	}
	

	
	public static int doLogicalStringComparison(java.lang.String string, java.lang.String string2)
	{
		Double lNum=new Double(0),rNum= new Double(0);
		try
		{
			lNum=new Double(string);
			rNum = new Double(string2);
		}
		catch (Exception e)
		{
			return 0;
		}
		return lNum.compareTo(rNum);
	}

	
//	public static void writeModelsToCSV(String outputFileFullName, String[] keys, String[] serializedWorlds)
//	{
//		BufferedWriter writer=null;
//		try
//		{
//			writer = new BufferedWriter(new FileWriter(outputFileFullName));
//			writeModelKeysToCSV(writer,keys);
//			for (String serializedWorld:serializedWorlds)
//			{
//				writeModelStructureToCSV(writer,keys,serializedWorld);
//			}
//		}
//		catch (Exception e)
//		{
//			java.io.StringWriter traceText = new java.io.StringWriter();
//			java.io.PrintWriter pWriter = new java.io.PrintWriter(traceText,true);
//			e.printStackTrace(pWriter);
//			pWriter.close();
//			throw new RuntimeException(traceText.toString());
//		}
//		finally
//		{
//			if (writer!=null)
//			{
//				try
//				{
//					writer.close();
//				}
//				catch (Exception e)
//				{
//					
//				}
//			}
//		}
//	}
	
//	public static void writeModelsToCSV(String outputFileFullName, String[] serializedWorlds)
//	{
//		HashSet<String> keys = extractAllKeys(serializedWorlds);
//		writeModelsToCSV(outputFileFullName,keys.toArray(new String[0]),serializedWorlds);
//	}
	
	
	
	public static void writeModelKeysToCSV(BufferedWriter writer, String[] keys) throws IOException
	{
		StringBuilder sBuilder = new StringBuilder();
		int keyLength=keys.length;
		if (keyLength>0)
		{
			if (keys[0].length()>0)
			{
				sBuilder.append("\"");
				sBuilder.append(keys[0].replace((CharSequence)"\"",(CharSequence)"\"\""));
				sBuilder.append("\"");
			}
			
		}
		for (int i=1;i<keys.length;i++)
		{
			sBuilder.append(",");
			if (keys[i].length()>0)
			{
				sBuilder.append("\"");
				sBuilder.append(keys[i].replace((CharSequence)"\"",(CharSequence)"\"\""));
				sBuilder.append("\"");
			}
		}
		
		writer.write(sBuilder.toString());
		writer.newLine();
	}
	
//	public static void writeModelStructureToCSV(BufferedWriter writer,String[] keys, String structureLine) throws IOException
//	{
//		Hashtable<String,String> table=parseToKeyValueMap(structureLine);
//		
//		StringBuilder sBuilder = new StringBuilder();
//		int keyLength=keys.length;
//		if (keyLength>0)
//		{
//			if (table.containsKey(keys[0]))
//			{
//				sBuilder.append("\"");
//				sBuilder.append(table.get(keys[0]).replace((CharSequence)"\"",(CharSequence)"\"\""));
//				sBuilder.append("\"");
//			}
//			
//		}
//		for (int i=1;i<keys.length;i++)
//		{
//			sBuilder.append(",");
//			if (table.containsKey(keys[i]))
//			{
//				sBuilder.append("\"");
//				sBuilder.append(table.get(keys[i]).replace((CharSequence)"\"",(CharSequence)"\"\""));
//				sBuilder.append("\"");
//			}
//		}
//		
//		writer.write(sBuilder.toString());
//		writer.newLine();
//	}
	
	
	
	public static String[] splitCommaDelimitedString(String tokenizedString, char splitChar)
	{
		char[] chars = tokenizedString.toCharArray();
		StringBuilder word= new StringBuilder();
		LinkedList<String> words = new LinkedList<String>();
		
		char currentChar;
		
		
		boolean ignoringSplitToken=false;
		boolean parsingDelimitedQuoteP=false;
		boolean canParseDelimitedQuoteP=false;
		
		int totalChars=chars.length;
		for (int i=0;i<totalChars;i++)
		{
			canParseDelimitedQuoteP=i<totalChars-1;
			currentChar=chars[i];
			
			if (currentChar=='"')
			{
				if (parsingDelimitedQuoteP)
				{
					word.append(currentChar);
					parsingDelimitedQuoteP=false;
				}
				else
				{
					if ((canParseDelimitedQuoteP)&&(chars[i+1]=='"'))
						parsingDelimitedQuoteP=true;
					else
					{
						ignoringSplitToken=!ignoringSplitToken;
					}
				}
			}
			else
			{
				if (currentChar==splitChar)
				{
					if (ignoringSplitToken)
					{
						word.append(currentChar);
					}
					else
					{
						words.add(word.toString());
						word = new StringBuilder();
					}
				}
				else
					word.append(currentChar);
			}
			
		}
		words.add(word.toString());
		return words.toArray(new String[0]);
	}
	
	
	
}
